package com.fivestar.finance.library.widget.slidedatetimepicker;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import java.util.Date;

/**
 * <p>This class contains methods for the library client to create
 * a new {@code SlideTimePicker}.</p>
 * <p>
 * <p>It also implements a Builder API that offers more convenient
 * object creation.</p>
 *
 * @author derek
 */
public class SlideTimePicker {

    private FragmentManager mFragmentManager;
    private SlideTimeListener mListener;
    private Date mInitialTime;
    private boolean mIsClientSpecified24HourTime;
    private boolean mIs24HourTime;
    private int interval;

    /**
     * Creates a new instance of {@code SlideTimePicker}.
     *
     * @param fm The {@code FragmentManager} from the calling activity that is used
     *           internally to show the {@code DialogFragment}.
     */
    public SlideTimePicker(FragmentManager fm) {
        // See if there are any DialogFragments from the FragmentManager
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag(SlideDateTimeDialogFragment.TAG_SLIDE_DATE_TIME_DIALOG_FRAGMENT);

        // Remove if found
        if (prev != null) {
            ft.remove(prev);
            ft.commit();
        }

        mFragmentManager = fm;
    }

    /**
     * <p>Sets the listener that is used to inform the client when
     * the user selects a new date and time.</p>
     * <p>
     * <p>This must be called before {@link #show()}.</p>
     *
     * @param listener
     */
    public void setListener(SlideTimeListener listener) {
        mListener = listener;
    }

    public void setmInitialTime(Date mInitialTime) {
        this.mInitialTime = mInitialTime;
    }


    private void setIsClientSpecified24HourTime(boolean isClientSpecified24HourTime) {
        mIsClientSpecified24HourTime = isClientSpecified24HourTime;
    }

    /**
     * <p>Sets whether the TimePicker displays its time in 12-hour
     * (AM/PM) or 24-hour format.</p>
     * <p>
     * <p>If this method is not called, the device's default time
     * format is used.</p>
     * <p>
     * <p>This also affects the time displayed in the tab.</p>
     * <p>
     * <p>Must be called before {@link #show()}.</p>
     *
     * @param is24HourTime <tt>true</tt> to force 24-hour time format,
     *                     <tt>false</tt> to force 12-hour (AM/PM) time
     *                     format.
     */
    public void setIs24HourTime(boolean is24HourTime) {
        setIsClientSpecified24HourTime(true);
        mIs24HourTime = is24HourTime;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }

    /**
     * Shows the dialog to the user. Make sure to call
     * {@link #/setListener()} before calling this.
     */
    public void show() {
        if (mListener == null) {
            throw new NullPointerException(
                    "Attempting to bind null listener to SlideDateTimePicker");
        }


        SlideTimeDialogFragment dialogFragment =
                SlideTimeDialogFragment.newInstance(mListener,mInitialTime,
                        mIsClientSpecified24HourTime,
                        mIs24HourTime, interval);

        dialogFragment.show(mFragmentManager,
                SlideDateTimeDialogFragment.TAG_SLIDE_DATE_TIME_DIALOG_FRAGMENT);
    }

    /*
     * The following implements the builder API to simplify
     * creation and display of the dialog.
     */
    public static class Builder {
        // Required
        private FragmentManager fm;
        private SlideTimeListener listener;

        // Optional
        private Date initialTime;
        private boolean isClientSpecified24HourTime;
        private boolean is24HourTime;

        //时间-分钟间隔
        private int interval;

        public Builder(FragmentManager fm) {
            this.fm = fm;
        }

        public Builder setListener(SlideTimeListener listener) {
            this.listener = listener;
            return this;
        }


        /**
         * @see SlideTimePicker#setIs24HourTime(boolean)
         */
        public Builder setIs24HourTime(boolean is24HourTime) {
            this.isClientSpecified24HourTime = true;
            this.is24HourTime = is24HourTime;
            return this;
        }

        public Builder setInitialTime(Date initialTime) {
            this.initialTime = initialTime;
            return this;
        }

        public Builder setInterval(int interval) {
            this.interval = interval;
            return this;
        }

        /**
         * <p>Build and return a {@code SlideDateTimePicker} object based on the previously
         * supplied parameters.</p>
         * <p>
         * <p>You should call {@link #show()} immediately after this.</p>
         *
         * @return
         */
        public SlideTimePicker build() {
            SlideTimePicker picker = new SlideTimePicker(fm);
            picker.setListener(listener);
            picker.setmInitialTime(initialTime);
            picker.setIsClientSpecified24HourTime(isClientSpecified24HourTime);
            picker.setIs24HourTime(is24HourTime);
            picker.setInterval(interval);

            return picker;
        }
    }
}
